/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Class;

import com.hp.hpl.jena.util.FileManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Edwin, Cristian
 */
public class Search {

    public Search() {

    }

    public StringBuilder InitializeSearch(String urlFile, String term) {
        try {
            Document result = this.Read(urlFile);
            if (result != null && !term.equals("")) {
                return this.SearchIntoRootElement(result, term);
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Document Read(String inputFileName) throws ParserConfigurationException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document;
        try {
            document = builder.parse(new File(inputFileName));
            return document;
        } catch (IOException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public StringBuilder SearchIntoRootElement(Document document, String term) {
        if (document != null) {
            Element rootElement = document.getDocumentElement();
            StringBuilder result = new StringBuilder();

            if (rootElement != null) {
                String name = rootElement.getNodeName();
                if (name.contains(term)) {
                    result.append(Utils.elementToString(rootElement)).append("\n");
                } else {
                    NamedNodeMap attributes = rootElement.getAttributes();
                    for (int j = 0; j < attributes.getLength(); j++) {
                        String attrName = attributes.item(j).getNodeName();
                        String attrValue = attributes.item(j).getNodeValue();
                        if (attrName.contains(term) || attrValue.contains(term)) {
                            result.append(Utils.elementToString(rootElement)).append("\n");
                            break;
                        }
                    }
                }

                result.append(SearchIntoElements(rootElement, term));
                return result;
            }
        }

        return null;
    }

    public StringBuilder SearchIntoElements(Element rootElement, String term) {
        StringBuilder result = new StringBuilder();
        NodeList childs = rootElement.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node aChild = childs.item(i);
            String name = aChild.getNodeName();
            if (name.contains(term)) {
                result.append(Utils.elementToString(aChild)).append("\n");
            } else {
                NamedNodeMap childAttributes = aChild.getAttributes();
                if (childAttributes != null && childAttributes.getLength() > 0) {
                    for (int j = 0; j < childAttributes.getLength(); j++) {
                        String attrName = childAttributes.item(j).getNodeName();
                        String attrValue = childAttributes.item(j).getNodeValue();
                        if (attrName.contains(term) || attrValue.contains(term)) {
                            result.append(Utils.elementToString(aChild)).append("\n");
                            break;
                        }
                    }
                }
            }
        }

        return result;
    }
}
